<?php

include('../includes/smarty.php');
include_once('../config.php');

session_start();

$ADMIN = new Admin();
$EMPLOYER = new Employer();
$INDUSTRY = new Industry();

if(isset($_SESSION["id"]))
{
	//	Validate the session.
	$row = $ADMIN->validate_admin_session($_SESSION["id"]);
	
	if($row)
	{
		$ADMIN->update_lastvisit($_SESSION["id"]);
		
		if(empty($_POST)) 
		{
			SmartyValidate::connect($smarty, true);
	
		    SmartyValidate::register_validator('rv_companyName',	'companyName',			'notEmpty',	false, 	false, 'trim');
			SmartyValidate::register_validator('rv_address',		'address',				'notEmpty',	false, 	false, 'trim');
			SmartyValidate::register_validator('rv_city',		 	'city',					'notEmpty',	false, 	false, 'trim');
			SmartyValidate::register_validator('rv_zip',		 	'zip',					'isNumber',	false, 	false, 'trim');
			SmartyValidate::register_validator('rv_address2',	 	'address2',				'notEmpty',	true, 	false, 'trim');
			SmartyValidate::register_validator('rv_city2',		 	'city2',				'notEmpty',	true, 	false, 'trim');
			SmartyValidate::register_validator('rv_zip2',		 	'zip2',					'isNumber',	true, 	false, 'trim');
		    SmartyValidate::register_validator('rv_email', 			'email', 				'isEmail', 	false, 	false, 'trim');
		
			$smarty->assign('states', 	get_state_select());
			$smarty->assign('industry', $INDUSTRY->get_industry_select());
			$smarty->assign('zipREX', 	$regex_zip);
			$smarty->assign('phoneREX', $regex_phone);
			$smarty->assign('emailREX', $regex_email);		
			$smarty->assign('title', 'Admin: Add a new Company');
			$smarty->assign('path', $site);
			$smarty->assign('message', '');
			$smarty->assign('body', 'main/admin/poster.tpl');

			$smarty->display('admin.tpl');
		}
		else 
		{    
			SmartyValidate::connect($smarty);
    		if(SmartyValidate::is_valid($_POST)) 
			{
				// this post is valid.
		
				$compName	= $_POST["companyName"];	//posts companyname
				$industry 	= $_POST["industry"];		//posts industry
				$address 	= $_POST["address"];		//posts address
				$city 		= $_POST["city"];			//posts city
				$state 		= $_POST["state"];			//posts state
				$zipcode 	= $_POST["zip"];			//posts zipcode
				$address2	= $_POST["address2"];		//posts address
				$city2	 	= $_POST["city2"];			//posts city
				$state2 	= $_POST["state2"];			//posts state
				$zipcode2	= $_POST["zip2"];			//posts zipcode
				$phone 		= $_POST["phone"];			//posts phone
				$fax 		= $_POST["fax"];			//posts fax
				$website 	= $_POST["website"];		//posts website
				$email 		= $_POST["email"];			//posts email
		
				//	store entries in database.
				$EMPLOYER->add_company($compName, $industry, $address, $city, $state, $zipcode, $address2, $city2, $state2, $zipcode2, $phone, $fax, $website, $email);
		
				//	success
				$smarty->assign('states', 	get_state_select());
				$smarty->assign('industry', $INDUSTRY->get_industry_select());
				$smarty->assign('zipREX', 	$regex_zip);
				$smarty->assign('phoneREX', $regex_phone);
				$smarty->assign('emailREX', $regex_email);		
				$smarty->assign('title', 'Admin: Add a new Company');
				$smarty->assign('path', $site);
				$smarty->assign('message', 'Successfully added the company.');
				$smarty->assign('body', 'main/admin/poster.tpl');

				$smarty->display('admin.tpl');
			}
	   		else
	   		{
				$smarty->assign('states', 	get_state_select());
				$smarty->assign('industry', $INDUSTRY->get_industry_select());
				$smarty->assign('zipREX', 	$regex_zip);
				$smarty->assign('phoneREX', $regex_phone);
				$smarty->assign('emailREX', $regex_email);		
				$smarty->assign('title', 'Admin: Add a new Company');
				$smarty->assign('path', $site);
				$smarty->assign('message', '');
				$smarty->assign('body', 'main/admin/poster.tpl');

				$smarty->display('admin.tpl');
	   		}
		}
	}
	else
	{
		//	Kill the current session, and show the login screen.
		session_destroy();
		header('Location: ../admin');
	}
}
else
{
	//	Kill the current session, and show the login screen.
	session_destroy();
	header('Location: ../admin');
}

?>